<template>
    <div class="wrapper" :class="toggleClasses">
        <div class="toast" ref="toast">
            <div class="message">
                <slot v-if="!enabledHtml"></slot>
                <div v-else v-html="$slots.default[0]"></div>
            </div>
            <div class="line" ref="line"></div>
            <span class="close" v-if="closeButton" @click="onClickClose">
                {{closeButton.text}}
            </span>
        </div>
    </div>
</template>

<script>
  export default {
    name: "f-toast"
    , props: {
      //TODO 整合autoClose和autoCloseDelay
      autoClose: {
        type: Boolean
        , default: true
      }
      , autoCloseDelay: {
        type: Number
        , default: 50000
      }
      , closeButton: {
        type: Object
        //default值如果是个对象，必须以函数return
        , default: () => ({
          text: '关闭'
          , callback: () => {}
        })
      }
      , enabledHtml: {
        type: Boolean
        , default: false
      }
      , position: {
        type: String
        , default: 'top'
        , validator(value) {
          return ['top', 'bottom', 'middle'].indexOf(value) >= 0;
        }
      }
    }
    , mounted() {
      this.updateStyles();
      this.execAutoClose();
    }
    , computed: {
      toggleClasses() {
        return {[`position-${this.position}`]: true}
      }
    }
    , methods: {
      execAutoClose() {
        if (this.autoClose) {
          setTimeout(() => {
            this.close();
          }, this.autoCloseDelay)
        }
      },
      close() {
        this.$el.remove();
        this.$emit('close');
        this.$destroy(); //会将绑定的事件取消掉
      }
      , log() {
        console.log('测试');
      }
      , onClickClose() {
        this.close();
        this.closeButton.callback(this);
      }
      , updateStyles() {
        //TODO 在我们将toast的height改为min-height时，竖线的height 100%就失效了，我们需要用js来设置
        // this.$refs.line.style.height = this.$refs.toast.style.height; //style只获取内联

        //因为我们是现在plugin中先mount的，故mounted时，还没有css
        this.$nextTick(() => {
          this.$refs.line.style.height = this.$refs.toast.getBoundingClientRect().height + 'px';
        });
      }
    }
  }
</script>

<style lang="scss">
    $font-size: 14px;
    $toast-min-height: 40px;
    $toast-bg: rgba(0, 0, 0, .75);
    $animation-duration: 500ms;

    @keyframes slide-up {
        0% {opacity: 0;transform: translateY(100%);}
        100% {opacity: 1;transform: translateY(0%);}
    }

    @keyframes slide-down {
        0% {opacity: 0;transform: translateY(-100%);}
        100% {opacity: 1;transform: translateY(0%);}
    }

    @keyframes fade-in {
        0% {opacity: 0;}
        100% {opacity: 1;}
    }

    .wrapper{
        position: fixed;
        left: 50%;
        transform: translateX(-50%);

        &.position-top {
            top: 0;
            .toast{
                border-top-left-radius: 0;
                border-top-right-radius: 0;
                animation: slide-down $animation-duration;
            }
        }
        &.position-bottom {
            bottom: 0;
            .toast{
                border-bottom-left-radius: 0;
                border-bottom-right-radius: 0;
                animation: slide-up $animation-duration;
            }
        }
        &.position-middle {
            top: 50%;
            transform:translateX(-50%) translateY(-50%);
            .toast{
                animation: fade_in $animation-duration;
            }
        }
    }

    .toast {
        display: flex;
        align-items: center;

        font-size: $font-size;
        line-height: 1.8;
        min-height: $toast-min-height;

        border-radius: 4px;
        background: $toast-bg;
        box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, .5);
        color: white;
        padding: 0 16px;

        .message {
            padding: 8px 0;
        }
        .close {
            padding-left: 16px;
            flex-shrink: 0; //??设置了就不会换行了
        }
        .line {
            height: 100%;
            border-left: 1px solid #666;
            margin-left: 16px;
        }
    }
</style>
